

import Base from './base.js';

function isEmpty(obj) {
  return Object.keys(obj).length === 0 && obj.constructor === Object;
}

export default class extends Base {
  async getOwner(userid) {
    const model = this.model('user');
    const userinfo = await model.where(`id=${userid}`).find();
    return userinfo;
  }

  //获取用户信息
  async getuserinfoAction() {
  	const openid = this.get('openid');
  	if (!openid) {
  		this.error('该用户不存在');
      return false;
  	}
  	const model = this.model('user');
  	const data = await model.where(`openid="${openid}"`).find();
  	this.success(data);
  }

  //获取项目经理，技术经理列表
  async getteamleadersAction() {
  	const model = this.model('user');
  	const data = {};
    data.teamleaders = await model.where('isteamleader=1').select();
    data.manages = await model.where('ispm=1').select();
  	this.success(data);
  }

  //score 给人打分
  async scoreAction() {
  	const formdata = this.post();
    //console.log(formdata)
    const json = formdata.data;
    for (const item of json) {
      const result = await this.score(item);
      if (result == 1) {
        this.error('该用户不存在');
      } else if (result == 2) {
        this.error('不存在存在该项目');
      }
    }
  	this.success();
  }

  async score(formdata) {
    const pid = formdata.pid;
    const uid = formdata.uid;
    const score = formdata.score;
    if (!pid || !score || !uid) {
      return 1;
    }
    const model = this.model('product');
    const product_data = await model.where(`id="${pid}"`).find();
    if (isEmpty(product_data)) {
      return 2;
    }
    const product_user = this.model('product_user');
    const product_user_data = await product_user.where(`pid=${pid} and userid=${uid}`).find();
    if (isEmpty(product_user_data)) { //用户未加入该项目
      await product_user.add({ userid: uid, pid, score });
    } else {
      await product_user.where(`pid=${pid} and userid=${uid}`).update({ score });
    }
  }

  //项目经理打分列出组长的名单，组长打分，列出组员的名单
  async getscoreatAction() {
    const uid = this.get('uid');
    const pid = this.get('pid');
    if (!pid || !pid) {
      this.error();
      return false;
    }
    const usermodel = this.model('user');
    const product_user = this.model('product_user');
    const data = await usermodel.where(`id=${uid}`).find();
    if (data.ispm) {
      const pmodel = this.model('product');
      const product_data = await pmodel.where(`id="${pid}"`).find();
      const result = {};
      result.project = product_data;
      if (product_data.teamleader) {
        result.list = await usermodel.where(`id in (${product_data.teamleader})`).select();
        result.score = await product_user.where(`pid=${pid} and userid in (${product_data.teamleader})`).select();
      } else {
        result.list = null;
        result.score = null;
      }
      this.success(result);
    } else if (data.isteamleader) {
      const pmodel = this.model('product');
      const product_data = await pmodel.where(`id="${pid}"`).find();
      const result = {};
      result.project = product_data;
      const joins = await product_user.where(`pid=${pid}`).select();

      const uids = [];
      joins.forEach((item) => { uids.push(item.userid); });
      console.log(`id in (${uids.join()}) and isteamleader=0`);
      if (uids.length) {
        result.list = await usermodel.where(`id in (${uids.join()}) and team=${data.team} and isteamleader=0 and ispm=0 and ispmo=0`).select();
      } else {
        result.list = null;
      }
      result.score = joins;
      this.success(result);
    }
  }

  //score 给项目打分  项目经理给项目打分
  async scoreteamAction() {
  	const formdata = this.post();
  	const pid = formdata.pid;
  	const teamid = formdata.teamid;
  	const score = formdata.score;
  	if (!pid || !score || !score) {
  		this.error();
      return false;
  	}
  	const model = this.model('product_team');
  	const product_data = await model.where(`teamid="${teamid}" and pid="${pid}"`).find();
    if (isEmpty(product_data)) {
      const insertId = await model.add({ teamid, pid, score });
  		this.success('小组打分成功');
    } else {
      const affectedRows = await model.where(`teamid="${teamid}" and pid="${pid}"`).update({ score });
  		this.success(affectedRows);
    }
  }

  //修改项目状态 更改项目状态
  async setproductinfoAction() {
  	const formdata = this.post();
  	const pid = formdata.pid;
  	if (!pid) {
  		this.error();
      return false;
  	}
  	const title = formdata.data.title;
  	const owner = formdata.data.owner;
  	const productdescript = formdata.data.productdescript;
    const manager = formdata.data.manager;
    const teamleader = formdata.data.teamleader;
    const productdate = formdata.data.productdate;
    const status = formdata.data.status;
  	const model = this.model('product');
  	const product_data = await model.where(`id="${pid}"`).find();
    if (isEmpty(product_data)) {
      this.error('不存在存在该项目');
      return false;
    }
  	const affectedRows = await model.where(`id="${pid}"`).update({ title, owner, productdescript, manager, productdate, teamleader, status });
  	this.success('项目修改成功');
  }

  //创建项目
  async createproductAction() {
  	const formdata = this.post();
  	const title = formdata.title;
  	const owner = formdata.owner;
  	const productdescript = formdata.productdescript;
    const manager = formdata.manager;
    const teamleader = formdata.teamleader;
    const productdate = formdata.productdate;
  	const model = this.model('product');
  	const product_data = await model.where(`title="${title}"`).find();
  		if (!isEmpty(product_data)) {
  			this.error('存在该项目');
  			return false;
  		}
    const current = new Date();
  	const insertId = await model.add({ title, owner, productdescript, manager, productdate, teamleader, createtime: current.getTime() });
  	this.success('项目创建成功');
  }

  //加入项目
  async joinproductAction() {
  	const postjson = this.post();
  	const uid = postjson.uid;
  	const pid = postjson.pid;
  	const usermodel = this.model('user');
  	const product_usermodel = this.model('product_user');
  	if (!pid) {
  		this.error('项目信息不存在');
  		return false;
  	}
  	if (uid) {
  		const user_data = await usermodel.where(`id="${uid}"`).find();
  		if (isEmpty(user_data)) {
  			this.error('该用户不存在');
  			return false;
  		}
  		//直接加入
  		const product_user_data = await product_usermodel.where(`pid="${pid}" and userid="${uid}"`).find();
  		if (!isEmpty(product_user_data)) {
  			this.error('该用户已经加入该项目');
  			return false;
  		}
  		const insertId = await product_usermodel.add({ userid: uid, pid });
      if (insertId) {
        this.success('加入成功');
        return false;
      } else {
        this.error('用户加入项目失败');
        return false;
      }
  	} else {
  		//先创建用户
  		const formdata = postjson.data;
  		if (!formdata) {
  			this.error('用户创建数据错误');
  			return false;
  		}
  		const name = formdata.name;
  		const team = formdata.team;
  		const nickname = formdata.nickname;
      const openid = formdata.openid;
  		//let data = await usermodel.where('name="'+name+'"').find();
      //var addflag = true
      // if(!isEmpty(data)){
      //   if(data.openid){
      //     if(!isEmpty(data)){
      //       this.error('该用户已经存在');
      //       return false;
      //     }
      //   }else{
      //     let affectedRows = await usermodel.where('name="'+name+'"').update({openid: openid,nickname:nickname});
      //     addflag = false
      //   }
      // }
      let insertUser;
      //if(addflag){
  		  insertUser = await usermodel.add({ name, team, nickname, openid });
      //}else{
      //  insertUser = true
      //}
  		//if(insertUser){
  			const insertId = await product_usermodel.add({ userid: insertUser, pid });
  			if (insertId) {
  				this.success('加入成功');
  				return false;
  			} else {
  				this.error('用户加入项目失败');
  				return false;
  			}
  		// }else{
  		// 	this.error('用户创建失败');
  		// 	return false;
  		// }
  	}
  }
}
